package com.working.project.ui.main

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.observe
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.android.material.tabs.TabLayout
import com.working.libbase.base.BaseFragment
import com.working.libbase.model.ProjectTreeResponse
import com.working.libbase.route.RoutePath
import com.working.libcommon.adapter.setPagingAdapter
import com.working.libcommon.exts.cast
import com.working.project.R
import com.working.project.databinding.ProjectFragmentMainBinding
import org.koin.androidx.viewmodel.ext.android.viewModel

@Route(path = RoutePath.Project.MAIN_FRAGMENT)
class ProjectFragment : BaseFragment() {
    private lateinit var mBinding: ProjectFragmentMainBinding
    private val mViewModel: ProjectViewModel by viewModel()
    private val mAdapter: ProjectAdapter by lazy { ProjectAdapter(mViewModel.dataSource) }
    override fun onResume() {
        super.onResume()
    }

    override fun onDestroy() {
        super.onDestroy()
        activity?.cast<AppCompatActivity>()?.supportActionBar?.setDisplayShowCustomEnabled(false)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mBinding = binding(inflater, R.layout.project_fragment_main, container)
        return mBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        with(mBinding) {
            tabLayout.parent.cast<ViewGroup>()?.removeView(tabLayout)
            activity?.cast<AppCompatActivity>()?.supportActionBar?.also {
                it.setDisplayShowCustomEnabled(true)
            }?.customView = tabLayout
            immerseColor(Color.GREEN)
            recyclerView.setPagingAdapter(viewLifecycleOwner, mAdapter)
            mViewModel.projectTree.observe(viewLifecycleOwner) {
                it.data?.forEach { treeResponse ->
                    tabLayout.addTab(
                        tabLayout.newTab().setText(treeResponse.nameImpl()).setTag(treeResponse)
                    )
                }
                tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
                    override fun onTabReselected(tab: TabLayout.Tab?) {
                        recyclerView.scrollToPosition(0)
                    }

                    override fun onTabUnselected(tab: TabLayout.Tab?) {

                    }

                    override fun onTabSelected(tab: TabLayout.Tab?) {
                        tab?.tag?.cast<ProjectTreeResponse>()?.id?.let { cid ->
                            mViewModel.cid.set(cid)
                        }
                        mAdapter.refresh()
                    }
                })
            }
        }
    }
}